<?php
namespace app\Common\model;
use app\common\model\BaseModel;
use think\facade\Session;

/**
 * 管理员模型
 * Class AdminModel
 * @package app\Common\model
 */
class AdminAuthRuleModel extends BaseModel
{
    protected $name = 'admin_auth_rule';

    /**
     * 查询列表
     * @param array $where 提交
     * @param bool $type 类型,true分页查询,false查询所有
     */
    public static function list($where = [], $type = true)
    {
        if ($type) {
            return self::where($where)->paginate(10);
        } else {
            return self::where($where)->select();
        }
    }

    /**
     * 查询单条记录
     * @param $key 字段名
     * @param $value 字段值
     */
    public static function findByUsername($key, $value)
    {
        return self::where([trim($key) => trim($value)])->find();
    }

    /**
     * 添加记录
     * @param $data 数据
     */
    public static function addPost($data)
    {
        return self::create($data);
    }

    /**
     * 保存编辑
     * @param $where 条件
     * @param $data 数据
     * @return bool
     */
    public static function editSave($where, $data)
    {
        return self::where($where)->save($data);
    }

    /**
     * 删除
     * @param $where 条件
     * @return bool
     */
    public static function del($where)
    {
        return self::where($where)->delete();
    }

    /**
     * 获取权限列表
     * @param int $level
     * @return AdminAuthRuleModel[]|array|\think\Collection
     */
    public static function getList($level = 2)
    {
//        $list = self::getSub(0);//根节点
//        foreach ($list as $k => $v) {//模块
//
//            $list[$k]['sub'] = self::getSub($v['id']);
//
//            if (is_array($list[$k]['sub']) && $level == 3) {
//
//                foreach ($list[$k]['sub'] as $key => $value) {
//
//                    $list[$k]['sub'][$key]['sun'] = self::getSub($value['id']);
//                }
//            }
//        }

        $admin_group_id = Session::get('admin_group_id');
        $admin_id = Session::get('admin_id');
        $where = [];
        $where[0] = array('status', '>', 0);
        if($admin_id != 1){
            $rules = AdminAuthGroupModel::findByUsername('id', $admin_group_id);
            $where[] = array('id', 'in', $rules['rules']);
        }


        $arr = AdminAuthRuleModel::list($where, false);
        $list = AdminAuthRuleModel::getTree($arr->toArray());

        return $list;
    }

    /**
     * 获取父级权限列表
     * @param $pid 父级id
     * @param string $status
     */
    public static function getSub($pid, $status = '0')
    {
        $where[] = array('status', '>', 0);
        $where[] = array('pid', '=', $pid);
        $list = self::where($where)->order('id asc, sort desc')->select();

        $admin_group_id = Session::get('admin_group_id');
        $admin_id = Session::get('admin_id');

        $rules = AdminAuthGroupModel::findByUsername('id', $admin_group_id);
        $rule = explode(',', $rules['rules']);

        foreach ($list as $k => $v) {

            if (!in_array($v['id'], $rule) && $admin_id != 1) {
                unset($list[$k]);
            }
        }

        return $list;
    }

    /**
     * 获取列表
     */
    public static function getTree($array)
    {
        //第一步 构造数据
        $items = array();

        foreach($array as $value){

            $items[$value['id']] = $value;
        }

        //第二部 遍历数据 生成树状结构
        $tree = array();

        foreach($items as $key => $value){

            if(isset($items[$value['pid']])){

                $items[$value['pid']]['children'][] = &$items[$key];

            }else{

                $tree[] = &$items[$key];
            }

        }
        return $tree;
    }

}
